package cn.hxsteel.storage.module.tempturnover;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.hxsteel.jpart.pager.PagerVO;
import cn.hxsteel.model.bo.ViewBO;
import cn.hxsteel.storage.core.base.BaseController;
import cn.hxsteel.storage.core.config.constant.SheetConst;
import cn.hxsteel.storage.module.tempturnover.model.SheetVO;
import cn.hxsteel.storage.module.tempturnover.model.SyncVO;

/**
 * 出入库的控制器
 */
@Controller
@RequestMapping( "/tmp" )
public class TempturnoverController extends BaseController
{
    @Autowired
    private SyncService         syncService;
    @Autowired
    private TempturnoverService service;
    
    /**
     * 库存管理的主视图
     */
    @RequestMapping( "/depotManage" )
    public ModelAndView depotManageView( HttpServletRequest request )
    {
	ModelAndView res = new ModelAndView();
	String[] uriArray = new String[] { "/tmp/inDepot", "/tmp/outDepot", "/tmp/sync" };
	String viewName = getUserPermitViewName( uriArray, request );
	res.setViewName( viewName );
	return res;
    }
    
    @RequestMapping( "/sync" )
    public String sync()
    {
	return "/tempturnover/sync";
    }
    
    /**
     * 同步出库单的ajax调用
     */
    @RequestMapping( "/sync/do" )
    @ResponseBody
    public ViewBO doSync()
    {
	ViewBO viewBO = new ViewBO();
	
	syncService.syncGoodsList();
	syncService.syncSheet();
	
	SyncVO syncVO = syncService.giveSyncInfo();
	if ( syncVO.isSuccess() )
	{
	    viewBO.putOk( true );
	}
	else
	{
	    viewBO.putOk( false );
	    viewBO.putMessage( "同步失败！请稍候再试！" );
	}
	
	return viewBO;
    }
    
    @RequestMapping( "/outDepot" )
    public ModelAndView showOutPage( HttpServletRequest request )
    {
	return showPage( 1, request );
    }
    
    @RequestMapping( "/inDepot" )
    public ModelAndView showInPage( HttpServletRequest request )
    {
	return showPage( 0, request );
    }
    
    public ModelAndView showPage( int kind, HttpServletRequest request )
    {
	ModelAndView res = new ModelAndView();
	res.setViewName( "/tempturnover/list" );
	
	ViewBO viewBO = new ViewBO();
	
	SyncVO syncVO = syncService.giveSyncInfo();
	viewBO.put( "syncData", syncVO );
	
	String depot = service.giveDepotList();
	viewBO.put( "depot", depot );
	
	viewBO.put( "kind", kind );
	viewBO.put( "step", SheetConst.getStepKind() );
	
	res.addAllObjects( viewBO );
	
	return res;
    }
    
    /**
     * 更新出库单审核状态和汇总库存明细库位的ajax调用
     */
    /*@RequestMapping( "/verify/do" )
    @ResponseBody
    public JSONResponse doVerify( String sheets )
    {
    JSONResponse jResponse = new JSONResponse();
    JSONMessage jMessage = new JSONMessage( sheets );
    
    try
    {
        service.updateDepot( sheets.split( "-" ) );
        jResponse = ajaxCallSuccess( jMessage );
    }
    catch ( ProcExecFailException e )
    {
        jResponse = ajaxCallFail( e.getMessage() );
    }
    
    return jResponse;
    }*/
    
    /**
     * ajax 加载页面数据
     * 
     * @param kind
     * @param pageNo
     * @param fun
     */
    @RequestMapping( "/loadData/do" )
    @ResponseBody
    public ViewBO doLoadOutData( int kind, int pageNo, int pageSize )
    {
	ViewBO pageBO = new ViewBO();
	List< SheetVO > bill = service.giveBillData( pageNo, pageSize, kind );
	PagerVO pagerVO = service.createPagerData();
	pageBO.putData( bill );
	pageBO.putPager( pagerVO );
	return pageBO;
    }
    
    /**
     * 更新库存明细
     * 
     * @param sid
     * @param number
     * @param weight
     * @param depotID
     */
    @RequestMapping( "/submitSheetDetail/do" )
    public void doSubmitSheetDetail( int sid, int number, double weight, int depotID )
    {
	// try
	// {
	// service.updateSheetDetail( sid, depotID, number, weight );
	// }
	// catch ( ProcExecFailException e )
	// {
	// System.err.println( e.getMessage() );
	// }
    }
    
}
